<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Material extends CI_Controller {

	public function index()
	{
		is_login($this->session);
		
		$data = array(
			'version' => $this->config->config['QUOTE_VERSION'],
		);
		$material_type = $this->config->config['BASE_MATERIAL_TYPE'];
		
		// get请求参数
		$type = getdata($this->input->get('type'), 1);
		$data['type'] = $type;
		$keyword = getdata($this->input->get('keyword'));
		$data['search_item'] = array(
			'keyword' => $keyword
		);
		
		// 查询条件
		$where_arr = array('type=?');
		$cond = array($type);
		if ($keyword){
			$where_arr[] = 'title like "%'.$keyword.'%"';
		}
		if ($type == 2){
			$where_arr[] = 'multi_id=?';
			$cond[] = 0;
		}
		$where = implode(' and ', $where_arr);
		
		// 读取model层
		$this->load->model('Material_model', 'Mmaterial');
		$re_list = $this->Mmaterial->get_list($where, $cond);
		
		foreach ($re_list as &$item){
			$item['type_str'] = $material_type[$item['type']];
		}
		$data['material_list'] = $re_list;
		
		$this->load->view('material', $data);
		
	}
	
	public function edit(){
		is_login($this->session);
		set_time_limit(5 * 60);
		
		$data = array(
			'version' => $this->config->config['QUOTE_VERSION'],
		);
		$nowtime = date('Y-m-d H:i:s');
		
		// 处理提交数据
		if ($this->input->post()){
			$this->load->model('Material_model', 'Mmaterial');
			
			$id = getdata($this->input->post('id'), 0);
			$type = getdata($this->input->post('type'), 0);
			$multi_id = getdata($this->input->post('multi_id'), 0);
			$data_submit = array();
			// multi_id, type, title, author, cover, summary, content, picurl, voiceurl, videourl, add_time
			$data_submit[] = $multi_id;
			$data_submit[] = $type;
			if ($type == 1 || $type == 2){
				$upload_re = $this->Mmaterial->up_file($_FILES['cover'], $type);
				$data_submit[] = getdata($this->input->post('title'));
				$data_submit[] = getdata($this->input->post('author'));
				if ($upload_re['re']){
					$data_submit[] = $upload_re['file'];
				}else {
					$data_submit[] = '';
				}
				$data_submit[] = getdata($this->input->post('summary'));
				$data_submit[] = getdata($this->input->post('content'));
				$data_submit[] = '';
				$data_submit[] = '';
				$data_submit[] = '';
				$data_submit[] = $nowtime;
			}elseif ($type == 3){
				$upload_re = $this->Mmaterial->up_file($_FILES['picurl'], $type);
				$data_submit[] = getdata($this->input->post('title'));
				$data_submit[] = '';
				$data_submit[] = '';
				$data_submit[] = '';
				$data_submit[] = '';
				if ($upload_re['re']){
					$data_submit[] = $upload_re['file'];
				}else {
					$data_submit[] = '';
				}
				$data_submit[] = '';
				$data_submit[] = '';
				$data_submit[] = $nowtime;
			}elseif ($type == 4){
				$upload_re = $this->Mmaterial->up_file($_FILES['voiceurl'], $type);
				$data_submit[] = getdata($this->input->post('title'));
				$data_submit[] = '';
				$data_submit[] = '';
				$data_submit[] = '';
				$data_submit[] = '';
				$data_submit[] = '';
				if ($upload_re['re']){
					$data_submit[] = $upload_re['file'];
				}else {
					$data_submit[] = '';
				}
				$data_submit[] = '';
				$data_submit[] = $nowtime;
			}elseif ($type == 5){
				$upload_re = $this->Mmaterial->up_file($_FILES['videourl'], $type);
				$data_submit[] = getdata($this->input->post('title'));
				$data_submit[] = '';
				$data_submit[] = '';
				$data_submit[] = getdata($this->input->post('summary'));
				$data_submit[] = '';
				$data_submit[] = '';
				if ($upload_re['re']){
					$data_submit[] = $upload_re['file'];
				}else {
					$data_submit[] = '';
				}
				$data_submit[] = '';
				$data_submit[] = $nowtime;
			}else {
				exit('Type Error!');
			}
			
			if ($id == 0){
				// 添加数据
				if (!empty($upload_re['msg'])){
					exit($upload_re['msg']);
				}
				$optid = $this->Mmaterial->add($data_submit);
			}else{
				// 修改数据
				// 0multi_id, 1type, 2title, 3author, 4cover, 5summary, 6content, 7picurl, 8voiceurl, 9videourl, 10add_time
				$data_up = array();
				if ($type == 1 || $type == 2){
					$data_up['title'] = $data_submit[2];
					$data_up['author'] = $data_submit[3];
					$data_up['summary'] = $data_submit[5];
					$data_up['content'] = $data_submit[6];
					if (!empty($data_submit[4])){
						$data_up['cover'] = $data_submit[4];
					}
				}elseif ($type == 3){
					$data_up['title'] = $data_submit[2];
					if (!empty($data_submit[7])){
						$data_up['picurl'] = $data_submit[7];
					}
				}elseif ($type == 4){
					$data_up['title'] = $data_submit[2];
					if (!empty($data_submit[8])){
						$data_up['voiceurl'] = $data_submit[8];
					}
				}elseif ($type == 5){
					$data_up['title'] = $data_submit[2];
					$data_up['summary'] = $data_submit[5];
					if (!empty($data_submit[9])){
						$data_up['videourl'] = $data_submit[9];
					}
				}else {
					exit('Type Error!');
				}
				$this->Mmaterial->update($data_up, 'where id=?', array($id));
				$optid = $id;
			}
			
			// 上传文件到微信服务器
			if ($upload_re['re'] && $optid > 0){
				$type_arr = array(3 => 'image', 4 => 'voice', 5 => 'video');
				if (isset($type_arr[$type])){
					$typestr = $type_arr[$type];
					$this->load->model('Wxident_model', 'Mwxident');
					$access_token = $this->Mwxident->get_access_token();
					$up_url = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token={$access_token}&type={$typestr}";
					$file = BASEWEBPATH . $upload_re['file'];
					$re_up = $this->Mmaterial->upload_to_wx($up_url, $file, $typestr);
					$re_arr = json_decode($re_up, true);
					if (isset($re_arr['media_id']) && isset($re_arr['created_at'])){
						$updata_wx = array(
								'media_id' => $re_arr['media_id'],
								'media_id_time' => $re_arr['created_at']
							);
						$where_wx = 'id=?';
						$cond_wx = array($optid);
						$this->Mmaterial->update($updata_wx, $where_wx, $cond_wx);
					}
					// 处理上传结果
				}
			}
			
			if ($multi_id > 0){
				redirect(BASEWEBSITE . 'material/multi?multi_id=' . $multi_id);
			}else{
				redirect(BASEWEBSITE . 'material?type=' . $type);
			}
		}
		
		// 接收处理修改请求
		$id = getdata($this->input->get('id'), 0);
		if ($id > 0){
			// 修改
			$data['mid'] = $id;
			$this->load->model('Material_model', 'Mmaterial');
			$re_material = $this->Mmaterial->get_list('id=?', array($id));
			if (isset($re_material[0])){
				$data['material'] = $re_material[0];
				$type = $re_material[0]['type'];
			}else{
				exit('Read id(' . $id . ') Error!');
			}
		}else{
			// 添加
			$type = getdata($this->input->get('type'), 0);
			$data['multi_id'] = getdata($this->input->get('multi_id'), 0);
		}
		$data['type'] = $type;
		
		$this->load->view('material_edit', $data);
		
	}
	
	public function del(){
		is_login($this->session);
		
		$id = getdata($this->input->get('id'));
		if ($id){
			$this->load->model('Material_model', 'Mmaterial');
			$re_del = $this->Mmaterial->del('id=?', array($id));
		}
		
		$previous_url = $_SERVER['HTTP_REFERER'];
		redirect($previous_url);
	}
	
	public function multi(){
		is_login($this->session);

		$data = array(
				'version' => $this->config->config['QUOTE_VERSION'],
		);
		$multi_id = getdata($this->input->get('multi_id'), 0);
		$data['multi_id'] = $multi_id;
		
		$where = 'id=? or multi_id=?';
		$cond = array($multi_id, $multi_id);
		
		$this->load->model('Material_model', 'Mmaterial');
		$re_list = $this->Mmaterial->get_list($where, $cond);
		
		$data['material_list'] = $re_list;
		$this->load->view('material_multi', $data);
	}
	
	public function detail(){
		$data = array(
			'version' => $this->config->config['QUOTE_VERSION'],
		);
		$data['errmsg'] = '';
		
		$id = getdata($this->input->get('id'), 0);
		if (!$id){
			$data['errmsg'] = '参数错误！';
		}else{
			$this->load->model('Material_model', 'Mmaterial');
			$material = $this->Mmaterial->get_list('id=?', $id);
			
			if (isset($material[0])){
				$data['material'] = $material[0];
			}else{
				$data['errmsg'] = '文章不存在！';
			}
		}
		
		$this->load->view('material_detail', $data);
		
	}
	
}
